package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.MyMessageRelation;
import cn.wolfcode.trip.base.mapper.CommentMapper;
import cn.wolfcode.trip.base.mapper.MyMessageRelationMapper;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ICommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentMapper commentMapper ;
    @Autowired
    private MyMessageRelationMapper myMessageRelationMapper;//我的信息


    @Override
    public void save(Comment comment) {
        //新建一个我的消息的对象
        MyMessageRelation mmr = new MyMessageRelation();
        //设置相对应的值
        mmr.setMessagetype(MyMessageRelation.MESSAGETYPE_IN_COMMENT);
        mmr.setCurrentuser(comment.getUser());//设置当前操作的用户




        //传递进来的值如果有空的话会报错，手动设置默认值把，如日报的id，和点评的id
        if(Comment.COMMENTTYPE_IN_TRAVEL==comment.getCommenttype()){
            mmr.setTravel(comment.getTravel());//设置被评论的游记
            //如果是评论了游记，就到我的消息里面新增一条记录
            myMessageRelationMapper.insertCommentTravel(mmr);//最后再新增数据到我的信息表中
        }

        if(Comment.COMMENTTYPE_IN_COMMENT==comment.getCommenttype()){
            commentMapper.insert(comment);//如果是评论了评论的话，就先新增，拿到子评论的id
            mmr.setChildComment(comment);
            //如果是评论了评论的话就新增一条信息
            myMessageRelationMapper.insertCommentComment(mmr);
            return ;//新增了评论之后就不要再新增了，我去，搞死我了
        }
        //新增
        commentMapper.insert(comment);
    }

    @Override
    public PageInfo queryForLits(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Comment> list = commentMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public Integer getCommentTotal(CommentQueryObject qo) {
        return commentMapper.selectCommentTotalByStratrgyCommentId(qo);
    }
}
